/**
 * Created by Administrator on 2017/6/27.
 */
define(['jquery', 'vue', 'ks-spa'], function ($, Vue,spa) {
    var keyword = "";
    var order = "id";
    var sort = "desc";
    var data = {
        'order': order,
        "sort": sort,
        "keyword": keyword,
        'page': 1
    };

    mui.init({
        pullRefresh: {
            container: '#refreshContainer',
            down: {
               // height: 1,//可选,默认50.触发下拉刷新拖动距离,
               // style:'circle',//必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
                auto: false,//可选,默认false.自动下拉刷新一次
                 contentdown: "",//可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
                 contentover: "正在刷新...",//可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
                 contentrefresh: "正在刷新...",//可选，正在刷新状态时，下拉刷新控件上显示的标题内容
                callback: pulldownRefresh//必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
            },
            up: {
                contentrefresh: '正在加载...',
                contentnomore: '',//可选，请求完毕若没有更多数据时显示的提醒内容；
                callback: pullupRefresh
            }
        }
    });
    function refreshContainer() {
        mui('#refreshContainer').pullRefresh().refresh(true);
        mui('#refreshContainer').pullRefresh().scrollTo(0,0);
        reloadData();
    }
    var tab_vue = new Vue({
        el: '#tab-head',
        data: {
            t_index: 0
        },
        methods: {
            tab_0: function (t_i) {
                if (vue.t_index !== t_i) {
                    data = {
                        'order': order,
                        "sort": sort,
                        "keyword": keyword,
                        'page': 1
                    };
                    tab_vue.t_index = t_i;
                    refreshContainer();
                }
            },
            tab_1: function (t_i) {
                if (vue.t_index !== t_i) {
                    data = {
                        'order': order,
                        "sort": sort,
                        "keyword": keyword,
                        'page': 1,
                        "flag_pay": 0,
                        "flag_delivery": 0
                    };
                    tab_vue.t_index = t_i;
                    refreshContainer();
                }
            },
            tab_2: function (t_i) {
                if (vue.t_index !== t_i) {
                    data = {
                        'order': order,
                        "sort": sort,
                        "keyword": keyword,
                        'page': 1,
                        "flag_status": 0
                    };
                    tab_vue.t_index = t_i;
                    refreshContainer();
                }
            },
            tab_3: function (t_i) {
                if (vue.t_index !== t_i) {
                    data = {
                        'order': order,
                        "sort": sort,
                        "keyword": keyword,
                        'page': 1,
                        "flag_status": 1
                    };
                    tab_vue.t_index = t_i;
                    refreshContainer();
                }
            }
            // tab_4: function (t_i) {
            //     if (vue.t_index !== t_i) {
            //         data = {
            //             'order': order,
            //             "sort": sort,
            //             "keyword": keyword,
            //             'page': 1,
            //             "flag_status": 7
            //         };
            //         tab_vue.t_index = t_i;
            //         refreshContainer();
            //     }
            // }
        }
    });
    var vue = new Vue({
        el: '#order_div',
        data: {
            items: []
        },
        methods: {
            order_pay: function (id, send_status) {
                if (send_status.code === 1) {
                    // mui.alert("每单配送费为配送员送达后收取（5元起）", '系统提示', function () {
                    //     window.location.href = order_pay_url + "/order_id/" + id;
                    // }, 'div');
                    mui.confirm('配送费5元起', '系统提示', ['取消', '确定'], function (e) {
                        if (e.index === 1) {
                            window.location.href = order_pay_url + "?order_id=" + id+"&back_url=" + c_url;
                        }
                    })
                }
                else {
                    mui.alert(send_status.msg);
                }
            },
            cancel_order: function (id) {
                mui.confirm('您确定取消该订单吗？', '系统提示', ['取消', '确定'], function (e) {
                    if (e.index === 1) {
                        $.post(cancel_order_url, {order_id: id}, function (res) {
                            if (res.code === 1) {
                                reloadData();
                            } else {
                                mui.alert(res.msg);
                            }
                        })
                    }
                })
            },
            take_order: function (id) {//确认收款
                mui.confirm('您确定收到货了吗？', '', ['取消', '确定'], function (e) {
                    if (e.index === 1) {
                        $.post(take_order_url, {order_id: id},
                            function (result) {
                                if (result.code === 1) {
                                    mui.alert("已确认收货!", '系统提示', function () {
                                        tab_vue.tab_0(0);
                                    }, 'div');
                                } else {
                                    mui.alert(result.msg);
                                }
                            });
                    }
                });
            },
            detail: function (id) {
                window.location.href = detail_url + "/order_id/" + id;
            }
        }
    });
    getData("up");
    function getData(flag) {
        $.get(list_url, data,
            function (result) {
                spa.hideLoading();
                if (result.code === 1) {
                    switch (flag) {
                        case "up":
                            if (result.data.length > 0) {
                                vue.items.push(result.data);
                                data.page++;
                                $("#tip-icon").hide();
                                mui('#refreshContainer').pullRefresh().endPullupToRefresh();
                            }
                            else {
                                if(data.page===1){
                                    $("#tip-icon").show();
                                    mui('#refreshContainer').pullRefresh().endPullupToRefresh();
                                }else{
                                    mui.toast("没有更多订单了");
                                    mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                                }
                            }
                            break;
                        case "down":
                            mui('#refreshContainer').pullRefresh().endPulldownToRefresh();
                            break;
                    }
                }
            });
    }

    // <editor-fold defaultstate="collapsed" desc="刷新数据">
    function reloadData() {
        data.page = 1;
        vue.items = [];
        getData("up");
    }

    // </editor-fold>
    /**
     * 上拉加载具体业务实现
     */
    function pullupRefresh() {
        getData("up");
    }

    /**
     * 下拉刷新具体业务实现
     */
    function pulldownRefresh() {
        spa.showLoading();
        switch (tab_vue.t_index){
            case 0:
                tab_vue.tab_0(tab_vue.t_index);
                break;
            case 1:
                tab_vue.tab_1(tab_vue.t_index);
                break;
            case 2:
                tab_vue.tab_2(tab_vue.t_index);
                break;
            case 3:
                tab_vue.tab_3(tab_vue.t_index);
                break;
        }
    }
});